import QtQuick 1.0
import Kookaburra 1.0

//Item {
World {
    property int screenState: 0

    id: game

    width: 800
    height: 600
    focus: true

    Image {
        anchors.fill: parent
        source: "res/img/title.png"
    }

    FontLoader {
        id: charlemagne
        source: "res/fnt/CharlemagneStd-Bold.otf"
    }

    FontLoader {
        id: artifika
        source: "res/fnt/Artifika-Regular.ttf"
    }

    Audio {
        id: bgTitle;
        source: "res/sfx/bg_title.wav"
    }

    Audio {
        id: bgEffect;
        source: "res/sfx/bg_eff.wav"
    }

    function resortItems() {
        world.generateChests();
    }

    function anyKeyOnTitle() {
        menu.opacity = 1;
        screenState = 1;
    }

    function beginGame() {
        var component = Qt.createComponent("Level.qml");
        if (component.status == Component.Ready) {
            bgEffect.stop();
            component.createObject(game);
        }
    }

    function enterOnMenu() {
        if(selector.state == "credits") {
            credits.opacity = 1;
            screenState = 2;
        } else {
            var component = Qt.createComponent("Intro.qml");
            if (component.status == Component.Ready) {
                bgTitle.stop();
                bgEffect.playWithLoop();
                var  introObj = component.createObject(game);
                introObj.completed.connect(beginGame);
                screenState = 3;
            }
        }
    }

    function anyKeyOnCredits(){
        console.debug("ou");
        credits.opacity = 0;
        screenState = 1;
    }

    Keys.onPressed: {
        switch (screenState) {
        case 0:
            anyKeyOnTitle();
            break;
        case 1:
            switch (event.key) {
            case Qt.Key_Up:
            case Qt.Key_Down:
                selector.state = (selector.state == "play" ? "credits" : "play");
                break;
            case Qt.Key_Enter:
            case Qt.Key_Return:
                enterOnMenu();
                break;
            }
            break;
        case 2:
            anyKeyOnCredits();
            break;
        }
    }

    Component.onCompleted: {
        bgTitle.playWithLoop();
    }

    Item {
        id : menu
        opacity: 0

        Behavior on opacity {
            NumberAnimation { duration: 1000 }
        }

        Text {
            text: "PLAY"
            font.family: charlemagne.name
            font.pixelSize: 32
            x: 545
            y: 380
            color: "#694A23"
        }

        Text {
            text: "CREDITS"
            font.family: charlemagne.name
            font.pixelSize: 32
            color: "#694A23"

            x: 545
            y: 430
        }

        Rectangle {
            id: selector
            width: 20
            height: 20
            color: "#694A23"
            state: "play"

            states: [
                State {
                    name: "play"
                    PropertyChanges { target: selector; x: 520; y: 389 }
                },
                State {
                    name: "credits"
                    PropertyChanges { target: selector; x: 520; y: 439 }
                }
            ]
        }
    }

    Item {
        id: credits
        visible: true
        opacity: 0


        Behavior on opacity {
             NumberAnimation { duration: 600 }
        }

        anchors.fill: parent

        Image {
            source: "res/img/credits_background.png"
        }

        Text {
            text: "Daniel Urbine\nEduardo Henrique\nFernando Siqueira\nMarie Fracca\nTalita Roberti"

            font.family: charlemagne.name
            font.pixelSize: 24
            anchors.centerIn: parent
            color: "#694A23"
        }
    }

    //Intro {}
}
